package com.adms.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.adms.domain.Message;
import com.adms.domain.MessageMapper;
import com.adms.domain.Reform;
import com.adms.domain.ReformMapper;
import com.adms.domain.ReformReply;
import com.adms.domain.ReformReplyExample;
import com.adms.domain.ReformReplyExample.Criteria;
import com.adms.domain.ReformReplyMapper;
import com.adms.util.AdmsConfig;
import com.adms.websocket.WebSocketServer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;

@Transactional
@Service
public class ReformReplyService {

	@Autowired
	private ReformReplyMapper reformReplyMapper;
	@Autowired
	private ReformMapper reformMapper;
	@Autowired
	private MessageMapper messageMapper;
	@Autowired
	private WebSocketServer webSocketServer;

	public ReformReply getByReplyId(Integer replyId) {
		return reformReplyMapper.selectByPrimaryKey(replyId);
	}

	public Map<String, Object> getByReplyIdWithReform(Integer replyId) {
		return reformReplyMapper.selectByPrimaryKeyWithReform(replyId);
	}

	public PageInfo<ReformReply> list(ReformReply reformReply, String timeField, Date start, Date end,
			String orderByClause, Integer pageNum, Integer pageSize, Integer navigatePages) {
		ReformReplyExample ex = constructExample(reformReply, timeField, start, end, orderByClause);

		PageHelper.startPage(pageNum, pageSize);
		List<ReformReply> replys = reformReplyMapper.selectByExample(ex);
		return new PageInfo<>(replys, navigatePages);
	}

	private ReformReplyExample constructExample(ReformReply reformReply, String timeField, Date start, Date end,
			String orderByClause) {
		ReformReplyExample ex = new ReformReplyExample();
		Criteria or = ex.or();
		if (reformReply != null) {
			Integer reformId = reformReply.getReformId();
			Boolean isGridReplied = reformReply.getIsGridReplied();
			Boolean isHandleReplied = reformReply.getIsHandleReplied();
			Integer gridAuditState = reformReply.getGridAuditState();
			Integer handleAuditState = reformReply.getHandleAuditState();
			Integer gridUserId = reformReply.getGridUserId();
			Integer handleUserId = reformReply.getHandleUserId();
			if (reformId != null) {
				or.andReformIdEqualTo(reformId);
			}
			if (isGridReplied != null) {
				or.andIsGridRepliedEqualTo(isGridReplied);
			}
			if (isHandleReplied != null) {
				or.andIsHandleRepliedEqualTo(isHandleReplied);
			}
			if (gridAuditState != null) {
				or.andGridAuditStateEqualTo(gridAuditState);
			}
			if (handleAuditState != null) {
				or.andHandleAuditStateEqualTo(handleAuditState);
			}
			if(gridUserId != null) {
				or.andGridUserIdEqualTo(gridUserId);
			}
			if(handleUserId != null) {
				or.andHandleUserIdEqualTo(handleUserId);
			}
		}

		if (StringUtil.isNotEmpty(timeField)) {
			if (timeField.equals("createTime")) {
				if (start != null) {
					if (end != null) {
						or.andCreateTimeBetween(start, end);
					} else {
						or.andCreateTimeGreaterThanOrEqualTo(start);
					}
				} else {
					if (end != null) {
						or.andCreateTimeLessThanOrEqualTo(end);
					}
				}
			}
			if (timeField.equals("gridReplyTime")) {
				if (start != null) {
					if (end != null) {
						or.andGridReplyTimeBetween(start, end);
					} else {
						or.andGridReplyTimeGreaterThanOrEqualTo(start);
					}
				} else {
					if (end != null) {
						or.andGridReplyTimeLessThanOrEqualTo(end);
					}
				}
			}
			if (timeField.equals("handleReplyTime")) {
				if (start != null) {
					if (end != null) {
						or.andHandleReplyTimeBetween(start, end);
					} else {
						or.andHandleReplyTimeGreaterThanOrEqualTo(start);
					}
				} else {
					if (end != null) {
						or.andHandleReplyTimeLessThanOrEqualTo(end);
					}
				}
			}
		}

		if (StringUtil.isNotEmpty(orderByClause)) {
			ex.setOrderByClause(orderByClause);
		} else {
			ex.setOrderByClause("reform_reply.create_time desc");
		}
		return ex;
	}

	public boolean audit(Integer replyId, Integer reformId, Integer gridAuditState, Integer handleAuditState) {
		ReformReply reformReply = reformReplyMapper.selectByPrimaryKey(replyId);
		Reform reform = reformMapper.selectByPrimaryKey(reformId);
		Message message = null;
		if (gridAuditState != null) {
			reformReply.setGridAuditState(gridAuditState);
			message = new Message();
			message.setUserId(reformReply.getGridUserId());
			message.setReformId(reformId);
			message.setTitle(reform.getTitle());
			if (gridAuditState == 1) {
				message.setType(AdmsConfig.MESSAGE_TYPE_AGREEREFORMREPLY);
			} else if (gridAuditState == 2) {
				reformReply.setIsGridReplied(false);
				message.setType(AdmsConfig.MESSAGE_TYPE_REJECTREFORMREPLY);
			}
			messageMapper.insertSelective(message);
			// 发送webSocket消息
			webSocketServer.sendMessage(reformReply.getGridUserId(), message);
			reformReplyMapper.updateByPrimaryKey(reformReply);
		}
		if (handleAuditState != null) {
			reformReply.setHandleAuditState(handleAuditState);
			message = new Message();
			message.setUserId(reformReply.getHandleUserId());
			message.setReformId(reformId);
			message.setTitle(reform.getTitle());
			if (handleAuditState == 1) {
				message.setType(AdmsConfig.MESSAGE_TYPE_AGREEREFORMREPLY);
			} else if (handleAuditState == 2) {
				reformReply.setIsHandleReplied(false);
				message.setType(AdmsConfig.MESSAGE_TYPE_REJECTREFORMREPLY);
			}
			messageMapper.insertSelective(message);
			// 发送webSocket消息
			webSocketServer.sendMessage(reformReply.getHandleUserId(), message);
			reformReplyMapper.updateByPrimaryKey(reformReply);
		}
		return true;
	}

}
